package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class Func8<T1,T2,T3,T4,T5,T6,T7,T8, TResult> extends Delegate<IFunc8<T1,T2,T3,T4,T5,T6,T7,T8, TResult>> {

    public Func8(IFunc8<T1,T2,T3,T4,T5,T6,T7,T8, TResult> delegate) {
        super(delegate);
    }

    public TResult invoke(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,T8 t8) throws Exception{
        TResult result = null;
        for (int i = 0; i < delegates.size(); i++) {
            result = delegates.get(i).invoke(t1,t2,t3,t4,t5,t6,t7,t8);
        }
        return result;
    }

    public AsyncResult<TResult> invokeAsync(T1 t1,T2 t2,T3 t3,T4 t4,T5 t5,T6 t6,T7 t7,T8 t8, @Nullable AsyncDelegateCallback callback) {
        ArrayList<FutureTask<TResult>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(t1,t2,t3,t4,t5,t6,t7,t8, null));
        }

        waitTasksFinish(tasks, new Object[]{t1,t2,t3,t4,t5,t6,t7,t8}, callback);

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}